package GameStates;

import com.badlogic.gdx.Gdx;
import com.me.mygdxgame.RacingGame;
import com.me.mygdxgame.renderer.AssetsManager;

public class StateManager
{
	private static StateManager instance = null;
	private BaseState currentState = null;
	private RacingGame copy = null;
	
	enum States
	{
		MAIN_MENU, GAMEPLAY, MAX_STATES
	};

	private StateManager()
	{
	}
	
	public void setGame(RacingGame game)
	{
		copy = game;
	}

	public static StateManager getInstance()
	{
		if (instance == null)
		{
			instance = new StateManager();
		}

		return instance;
	}

	public void changeState(BaseState state)
	{
		if(state == null)
			return;
		
		if(copy == null)
		{
			Gdx.app.log("StateManager", "Game was not initialized");
			return;
		}
		
		if (currentState != null)
		{
			currentState.dispose();
		}
		
		while(!AssetsManager.getInstance().update())
		{
			//wait
		}
		
		currentState = state;
		copy.setScreen(currentState);
	}
	
	public void update(float dt)
	{
		if(currentState.isDone())
		{
			handleStateChange();
		}
		
		if(currentState != null)
		{
			currentState.render(dt);
		}
	}
	
	private void handleStateChange()
	{
		switch(currentState.ID)
		{
			case MAIN_MENU:
			{
				changeState(new GameState());
				break;
			}
		}
	}


}
